using System.ComponentModel.DataAnnotations.Schema;

namespace WebApplication1.Models
{
    /// <summary>
    /// 通用返回结果封装类
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    [NotMapped]
    public class Result<T>
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 数据
        /// </summary>
        public T? Data { get; set; }

        /// <summary>
        /// 创建成功结果
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="message">消息</param>
        /// <returns>返回结果</returns>
        public static Result<T> Ok(T? data = default, string message = "操作成功")
        {
            return new Result<T>
            {
                Code = 200,
                Message = message,
                Data = data
            };
        }

        /// <summary>
        /// 创建失败结果
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        /// <returns>返回结果</returns>
        public static Result<T> Error(string message = "操作失败", int code = 500)
        {
            return new Result<T>
            {
                Code = code,
                Message = message,
                Data = default
            };
        }
    }

    /// <summary>
    /// 无数据的返回结果封装类
    /// </summary>
    public class Result
    {
        /// <summary>
        /// 创建成功结果
        /// </summary>
        /// <param name="message">消息</param>
        /// <returns>返回结果</returns>
        public static Result<object> Ok(string message = "操作成功")
        {
            return Result<object>.Ok(null, message);
        }

        /// <summary>
        /// 创建失败结果
        /// </summary>
        /// <param name="message">错误消息</param>
        /// <param name="code">错误码</param>
        /// <returns>返回结果</returns>
        public static Result<object> Error(string message = "操作失败", int code = 500)
        {
            return Result<object>.Error(message, code);
        }
    }
}